package init.auto;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Stream;

public class WalkLib {
	private final String home;
	private final String scanModuleName;

	public WalkLib(String home, String scanModuleName) {
		this.home = home;
		this.scanModuleName = scanModuleName;
	}

	public String libs() throws IOException {
		Path start = Paths.get(home, "modules");

		List<String> list = new ArrayList<String>();
		try (Stream<Path> walk = Files.walk(start, 2, FileVisitOption.values());) {
			list = walk.filter(path -> {
				if (path.getParent().endsWith(scanModuleName)) {
					return false;
				}
				if (path.toFile().getName().equals("lib") && path.toFile().listFiles(new JarExists()).length > 0) {
					return true;
				}
				return false;

			}).map(p -> {
				return p.toFile().getAbsolutePath().replace('\\', '/') + "/.*";
			}).toList();
		}

		List<String> listClasses = new ArrayList<String>();
		try (Stream<Path> walk = Files.walk(start, 2, FileVisitOption.values());) {
			listClasses = walk.filter(path -> {
				if (path.getParent().endsWith(scanModuleName)) {
					return false;
				}
				if (path.toFile().getName().equals("classes")) {
					return true;
				}
				return false;

			}).map(p -> {
				return p.toFile().getAbsolutePath().replace('\\', '/') + "/";
			}).toList();
		}

		List<String> all = new ArrayList<String>();
		all.addAll(listClasses);
		all.addAll(list);

		return String.join(File.pathSeparator, all);
	}


	public String libsAndClientLibs() throws IOException {
		Path start = Paths.get(home, "modules");

		List<String> list = new ArrayList<String>();
		try (Stream<Path> walk = Files.walk(start, 2, FileVisitOption.values());) {
			list = walk.filter(path -> {
				if (path.getParent().endsWith(scanModuleName)) {
					return false;
				}
				if (path.toFile().getName().equals("lib") && path.toFile().listFiles(new JarExists()).length > 0) {
					return true;
				}
				if (path.toFile().getName().equals("client") && path.resolve("lib").toFile().exists()
						&& path.resolve("lib").toFile().listFiles(new JarExists()).length > 0) {
					return true;
				}
				return false;

			}).map(p -> {
				return p.toFile().getAbsolutePath().replace('\\', '/').replace("client", "client/lib") + "/.*";
			}).toList();
		}

		List<String> listClasses = new ArrayList<String>();
		try (Stream<Path> walk = Files.walk(start, 2, FileVisitOption.values());) {
			listClasses = walk.filter(path -> {
				if (path.getParent().endsWith(scanModuleName)) {
					return false;
				}
				if (path.toFile().getName().equals("classes")) {
					return true;
				}
				return false;

			}).map(p -> {
				return p.toFile().getAbsolutePath().replace('\\', '/') + "/";
			}).toList();
		}

		List<String> all = new ArrayList<String>();
		all.addAll(listClasses);
		all.addAll(list);

		return String.join(File.pathSeparator, all);
	}



	private static class JarExists implements FilenameFilter {

		@Override
		public boolean accept(File dir, String name) {
			if (name.endsWith(".jar")) {
				return true;
			}
			return false;
		}
	}

}
